﻿/*
2021-03-05 JackLee
https://leetcode-cn.com/problems/implement-queue-using-stacks/



*/
using System.Collections.Generic;

namespace MyQueueUseStack
{
    public class MyQueue
    {
        Stack<int> stack1, stack2;

        /** Initialize your data structure here. */
        public MyQueue()
        {
            stack1 = new Stack<int>();
            stack2 = new Stack<int>();
        }

        /** Push element x to the back of queue. */
        public void Push(int x)
        {
            stack1.Push(x);
        }

        /** Removes the element from in front of queue and returns that element. */
        public int Pop()
        {
            if (Empty())
            {
                return -1;
            }

            //if (stack2.Count > 0)
            //    return stack2.Pop();
            //else
            //{
            //    while (stack1.Count>0)
            //    {
            //        int tmp = stack1.Pop();
            //        stack2.Push(tmp);
            //    }
            //    return stack2.Pop();
            //}

            if (stack2.Count==0)
            {
                while (stack1.Count>0)
                {
                    stack2.Push(stack1.Pop());
                }
            }
            return stack2.Pop();
        }

        /** Get the front element. */
        public int Peek()
        {
            if (Empty())
            {
                return -1;
            }

            //if (stack2.Count > 0)
            //    return stack2.Peek();
            //else
            //{
            //    while (stack1.Count>0)
            //    {
            //        int tmp = stack1.Pop();
            //        stack2.Push(tmp);
            //    }
            //    return stack2.Peek();
            //}
            if (stack2.Count==0)
            {
                while (stack1.Count>0)
                {
                    stack2.Push(stack1.Pop());
                }
            }
            return stack2.Peek();
        }

        /** Returns whether the queue is empty. */
        public bool Empty()
        {
            //if (stack1.Count == 0 && stack2.Count == 0)
            //{
            //    return true;
            //}
            //return false;
            return stack1.Count == 0 && stack2.Count == 0;
        }
    }

    /**
     * Your MyQueue object will be instantiated and called as such:
     * MyQueue obj = new MyQueue();
     * obj.Push(x);
     * int param_2 = obj.Pop();
     * int param_3 = obj.Peek();
     * bool param_4 = obj.Empty();
     */
}
